package ecustom.common;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import ecustom.common.conn.DB;
import ecustom.common.util.CustomUtil;
import weaver.soa.workflow.request.Cell;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;
import weaver.soa.workflow.request.Row;

public abstract class AbstractBaseAction {

	protected Logger log = Logger.getLogger(this.getClass());
	
	public String getSelectNameCK(int formId, String fieldName, Integer value, String errMsg) throws Exception {
		String sql = "SELECT ID FROM WORKFLOW_BILLFIELD WHERE BILLID=? AND UPPER(FIELDNAME)=?";
		Integer fieldId = DB.conn().getInteger(sql, formId, fieldName.toUpperCase());
		if (fieldId == null) throw new RuntimeException("字段名不存在，billId=" + formId + "，fieldName=" + fieldName);
		return getSelectNameCK(fieldId, value, errMsg);
	}
	
	public String getSelectNameCK(int fieldid, Integer value, String errMsg) throws Exception {
		if (value == null) throw new RuntimeException(errMsg);
		String sql = "SELECT SELECTNAME FROM WORKFLOW_SELECTITEM WHERE FIELDID=? AND SELECTVALUE=?";
		String name = DB.conn().getString(sql, fieldid, value);
		if (CustomUtil.isNotBlank(name)) return name;
		throw new RuntimeException(errMsg);
	}
	
	protected Logger log() {
		return this.log;
	}

	/**
	 * 获取主表数据，并转换成Integer类型值返回。
	 * @param fields
	 * @param fieldName
	 * @param defValue
	 * @return
	 */
	public static Integer getMainInteger(Property[] fields, String fieldName, Integer defValue) {
		String value = null;
		for (Property field : fields) {
			if (fieldName.equalsIgnoreCase(field.getName())) {
				value = field.getValue();
				break ;
			}
		}
		return CustomUtil.getInteger(value, defValue);
	}
	
	/**
	 * 获取主表数据，并转换成Double类型值返回。
	 * @param fields
	 * @param fieldName
	 * @param errMsg
	 * @return
	 */
	public static Double getMainDoubleCK(Property[] fields, String fieldName, String errMsg) {
		Double value = getMainDouble(fields, fieldName);
		if (value == null) throw new RuntimeException(errMsg);
		return value;
	}
	
	/**
	 * 获取主表数据，并转换成Double类型值返回。
	 * @param fields
	 * @param fieldName
	 * @return
	 */
	public static Double getMainDouble(Property[] fields, String fieldName) {
		return getMainDouble(fields, fieldName, null);
	}

	/**
	 * 获取主表数据，并转换成Double类型值返回。
	 * @param fields
	 * @param fieldName
	 * @param defValue
	 * @return
	 */
	public static Double getMainDouble(Property[] fields, String fieldName, Double defValue) {
		String value = null;
		for (Property field : fields) {
			if (fieldName.equalsIgnoreCase(field.getName())) {
				value = field.getValue();
				break ;
			}
		}
		return CustomUtil.getDouble(value, defValue);
	}
	
	/**
	 * 获取主表数据，并转换成String类型值返回。
	 * @param fields
	 * @param fieldName
	 * @return
	 */
	public static String getMainString(Property[] fields, String fieldName) {
		return getMainString(fields, fieldName, null);
	}
	
	public static String getMainStringCK(Property[] fields, String fieldName, String errMsg) {
		String value = getMainString(fields, fieldName, null);
		if (CustomUtil.isBlank(value)) throw new RuntimeException(errMsg);
		return value;
	}
	
	/**
	 * 获取主表数据，并转换成String类型值返回。
	 * @param fields
	 * @param fieldName
	 * @param defValue
	 * @return
	 */
	public static String getMainString(Property[] fields, String fieldName, String defValue) {
		String value = null;
		for (Property field : fields) {
			if (fieldName.equalsIgnoreCase(field.getName())) {
				value = field.getValue();
				break ;
			}
		}
		return CustomUtil.isBlank(value) ? defValue : value;
	}
	
	public static int getCellIntCK(Row row, String fieldName, String errMsg) {
		Integer value = getCellInteger(row, fieldName, null);
		if (value == null) throw new RuntimeException(errMsg);
		return value;
	}
	
	public static Integer getCellInteger(Row row, String fieldName) {
		return getCellInteger(row, fieldName, null);
	}
	
	/**
	 * 获取明细表数据，并转换成Integer类型值返回。
	 */
	public static Integer getCellInteger(Row row, String fieldName, Integer defValue) {
		if (CustomUtil.isBlank(fieldName)) return null;
		String value = null;
		Cell[] cells = row.getCell();
		for (Cell field : cells) {
			if (fieldName.equalsIgnoreCase(field.getName())) {
				value = field.getValue();
				break ;
			}
		}
		return CustomUtil.getInteger(value, defValue);
	}
	
	public static Double getCellDoubleCK(Row row, String fieldName, String errMsg) {
		Double value = getCellDoubleValue(row, fieldName);
		if (value == null) throw new RuntimeException(errMsg);
		return value;
	}
	
	public static Double getCellDoubleValue(Row row, String fieldName) {
		if (CustomUtil.isBlank(fieldName)) return null;
		String value = null;
		Cell[] cells = row.getCell();
		for (Cell field : cells) {
			if (fieldName.equalsIgnoreCase(field.getName())) {
				value = field.getValue();
				break ;
			}
		}
		return CustomUtil.getDouble(value, null);
	}
	
	public static String getCellString(Row row, String fieldName) {
		return getCellString(row, fieldName, "");
	}

	/**
	 * 获取明细表数据，并转换成String类型值返回。
	 */
	public static String getCellString(Row row, String fieldName, String defValue) {
		if (CustomUtil.isBlank(fieldName)) return "";
		String value = null;
		Cell[] cells = row.getCell();
		for (Cell field : cells) {
			if (fieldName.equalsIgnoreCase(field.getName())) {
				value = field.getValue();
				break ;
			}
		}
		return CustomUtil.isBlank(value) ? defValue : value;
	}
	
	public static String getCellStringCK(Row row, String fieldName) {
		return getCellStringCK(row, fieldName, "明细表“" + fieldName + "”不能为空！");
	}
	
	public static String getCellStringCK(Row row, String fieldName, String errMsg) {
		String value = getCellString(row, fieldName);
		if (CustomUtil.isBlank(value)) throw new RuntimeException(errMsg);
		return value;
	}

	public static Integer getMainInteger(Property[] fields, String fieldName) {
		return getMainInteger(fields, fieldName, null);
	}
	
	public static Integer getMainIntegerCK(Property[] fields, String fieldName, String errMsg) {
		Integer value = getMainInteger(fields, fieldName, null);
		if (value == null) throw new RuntimeException(errMsg);
		return value;
	}
	
	/**
	 * 获取指定明细表中的所有行。
	 * @param requestInfo
	 * @param detailIndex	明细表索引，下标从1开始。
	 * @return
	 * Create date: 2020年8月13日
	 */
	public static Row[] getRows(RequestInfo requestInfo, int detailIndex) {
		return requestInfo.getDetailTableInfo().getDetailTable(detailIndex - 1).getRow();
	}
	
	/**
	 * 获取指定明细表中的所有行。
	 * @param requestInfo
	 * @param detailIndex	明细表索引，下标从1开始。
	 * @param errMsg	异常消息文本，如果没有获取到明细行则抛出异常
	 * @return
	 * Create date: 2020年8月13日
	 */
	public static Row[] getRowsCK(RequestInfo requestInfo, int detailIndex, String errMsg) {
		Row[] rows = getRows(requestInfo, detailIndex);
		if (rows == null || rows.length == 0) throw new RuntimeException(errMsg);
		return rows;
	}
	
	public static CustomMap toMap(RequestInfo requestInfo) {
		String tableName = requestInfo.getRequestManager().getBillTableName();
		Property[] main = requestInfo.getMainTableInfo().getProperty();
		CustomMap map = new CustomMap();
		for (Property field : main) {
			map.put(field.getName().toUpperCase(), field.getValue());
		}
		map.put("ID", requestInfo.getRequestManager().getBillid());
		map.put("REQUESTID", requestInfo.getRequestManager().getRequestid());
		map.put("TABLENAME", tableName);
		return map;
	}
	
	public static List<CustomMap> toList(Row[] rows) {
		List<CustomMap> list = new ArrayList<CustomMap>();
		for (Row row : rows) {
			CustomMap map = new CustomMap();
			for (Cell field : row.getCell()) {
				map.put(field.getName().toUpperCase(), field.getValue());
			}
			map.put("ID", row.getId());
			list.add(map);
		}
		return list;
	}
	
	public static List<CustomMap> toList(RequestInfo requestInfo, int dtIndex) {
		int requestId = requestInfo.getRequestManager().getRequestid();
		String tableName = requestInfo.getRequestManager().getBillTableName();
		int billId = requestInfo.getRequestManager().getBillid();
		Row[] rows = requestInfo.getDetailTableInfo().getDetailTable(dtIndex).getRow();
		List<CustomMap> lines = toList(rows);
		for (CustomMap line : lines) {
			line.put("BILLID", billId);
			line.put("REQUESTID", requestId);
			line.put("TABLENAME", tableName);
		}
		return lines;
	}

	protected void updateMainForm(CustomMap main, CustomMap fields) throws Exception {
		String tableName = main.getStringCK("TABLENAME");
		String requestId = main.getStringCK("REQUESTID");
		DB.conn().update(tableName, fields, "REQUESTID=?", requestId);
		main.putAll(fields);
	}
}